<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>单据详情 - 虚拟仓系统</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
    <!-- 统一导航栏 -->
    {% include 'includes/navbar.html' %}

    <div class="container mt-4">
        <h3>单据详情</h3>
        <div class="card mb-4">
            <div class="card-body">
                <div class="row">
                    <div class="col-md-3"><strong>单据编号：</strong><span id="docId"></span></div>
                    <div class="col-md-2"><strong>类型：</strong><span id="docType"></span></div>
                    <div class="col-md-2"><strong>仓库：</strong><span id="warehouse"></span></div>
                    <div class="col-md-2"><strong>操作人：</strong><span id="operator"></span></div>
                    <div class="col-md-2"><strong>创建时间：</strong><span id="createTime"></span></div>
                    <div class="col-md-1"><strong>状态：</strong><span id="status" class="badge bg-success"></span></div>
                </div>
            </div>
        </div>
        <h4>商品明细</h4>
        <div class="card">
            <div class="card-body">
                <table class="table table-striped">
                    <thead>
                        <tr>
                            <th>商品ID</th>
                            <th>商品名称</th>
                            <th>数量</th>
                        </tr>
                    </thead>
                    <tbody id="itemsTable"></tbody>
                </table>
            </div>
        </div>
        <div class="mt-3">
            <a href="/stock/docs" class="btn btn-secondary">返回列表</a>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    <script>
        const getDocIdFromUrl = () => {
            const path = window.location.pathname;
            return path.split('/').pop();
        };

        // 加载单据详情（带/api前缀）
        const loadDocDetail = async () => {
            const docId = getDocIdFromUrl();
            const res = await fetch(`/api/stock/docs/${docId}`);
            const data = await res.json();
            if (data.status === 'error') {
                alert(data.message);
                return;
            }
            document.getElementById('docId').textContent = data.doc.doc_id;
            document.getElementById('docType').textContent = data.doc.doc_type;
            document.getElementById('warehouse').textContent = data.doc.warehouse;
            document.getElementById('operator').textContent = data.doc.operator;
            document.getElementById('createTime').textContent = data.doc.create_time;
            document.getElementById('status').textContent = data.doc.status;
            const itemsTable = document.getElementById('itemsTable');
            itemsTable.innerHTML = data.items.map(item => `
                <tr>
                    <td>${item.product_id}</td>
                    <td>${item.product_name}</td>
                    <td>${item.quantity}</td>
                </tr>
            `).join('');
        };

        document.getElementById('logout').addEventListener('click', () => window.location.href = '/login_page');
        window.onload = loadDocDetail;
    </script>
</body>
</html>